package com.demo2.todo.controller;

import com.demo2.todo.model.dto.ReportResponse;
import com.demo2.todo.model.dto.UserResponse;
import com.demo2.todo.model.entity.Report;
import com.demo2.todo.model.entity.Todo;
import com.demo2.todo.model.entity.User;
import com.demo2.todo.model.enums.ReviewStatus;
import com.demo2.todo.service.AdminService;
import com.demo2.todo.service.ReportService;
import com.demo2.todo.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author evi
 */
@RestController
@RequestMapping("/admin/users")
@RequiredArgsConstructor
@PreAuthorize("hasRole('ADMIN')")
public class AdminUserController {
    private final UserService userService;
    private final ReportService reportService;
    private final AdminService adminService;

    @GetMapping
    public ResponseEntity<List<UserResponse>> getAllUsers() {
        return ResponseEntity.ok(userService.getAllUsers().stream()
                .map(this::mapToDto)
                .collect(Collectors.toList()));
    }

    @PutMapping("/{id}/block")
    public ResponseEntity<Void> blockUser(@PathVariable Long id) {
        userService.blockUser(id);
        return ResponseEntity.noContent().build();
    }

    @PutMapping("/{id}/unblock")
    public ResponseEntity<Void> unblockUser(@PathVariable Long id) {
        userService.unblockUser(id);
        return ResponseEntity.noContent().build();
    }

    // 拉黑/解除拉黑用户
    @PatchMapping("/{id}/blacklist")
    public ResponseEntity<User> toggleUserBlacklist(
            @PathVariable Long id) {
        User updatedUser = userService.toggleBlacklist(id);
        return ResponseEntity.ok(updatedUser);
    }

    // 获取所有举报
    @GetMapping("/reports")
    public ResponseEntity<List<ReportResponse>> getAllReports(
            @RequestParam(required = false) Report.ReportStatus status) {
        List<ReportResponse> reports = reportService.getReportsByStatus(status);
        return ResponseEntity.ok(reports);
    }

    // 处理举报
    @PatchMapping("/reports/{id}/handle")
    public ResponseEntity<ReportResponse> handleReport(
            @PathVariable Long id,
            @RequestParam Report.ReportStatus status,
            @RequestParam(required = false) String comment) {
        // 验证状态合法性（只能处理为接受或驳回）
        if (status != Report.ReportStatus.ACCEPTED && status != Report.ReportStatus.rejected) {
            throw new IllegalArgumentException("处理状态只能是 ACCEPTED 或 rejected");
        }
        ReportResponse handledReport = reportService.handleReport(id, status, comment);
        return ResponseEntity.ok(handledReport);
    }

    // 审核待分享的Todo
    @PatchMapping("/todos/{id}/review")
    public ResponseEntity<?> reviewTodo(
            @PathVariable Long id,
            @RequestParam ReviewStatus status,
            @RequestParam(required = false) String comment) {
        adminService.reviewTodo(id, status, comment);
        return ResponseEntity.ok().build();
    }
    private UserResponse mapToDto(User user) {
        UserResponse response = new UserResponse();
        response.setId(user.getId());
        response.setUsername(user.getUsername());
        response.setEnabled(user.isEnabled());
        response.setRole(user.getRole());
        return response;
    }
}